package com.kupurui.asteacher.ui.job;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.android.frame.util.Toolkit;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.JobSelAdapter;
import com.kupurui.asteacher.adapter.ScreenRuleAdapter;
import com.kupurui.asteacher.bean.ChooseTypesInfo;
import com.kupurui.asteacher.bean.JobSelInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.utils.UserManger;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreContainer;
import in.srain.cube.views.ptr.loadmore.LoadMoreHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreListViewContainer;
import in.srain.cube.views.ptr.util.PtrInitHelper;
import info.hoang8f.widget.FButton;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 布置作业先预览 然后选择预览的作业发布
 * Created by Administrator on 2017/8/10.
 */

public class JobSelAty extends BaseAty implements PtrHandler, LoadMoreHandler {


    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.fbtn_preview)
    FButton fbtnPreview;
    @Bind(R.id.ll_but)
    LinearLayout llBut;
    @Bind(R.id.ll_difficulty)
    LinearLayout llDifficulty;
    @Bind(R.id.ll_unsel)
    LinearLayout llUnsel;
    @Bind(R.id.listview)
    ListView listview;
    @Bind(R.id.load_more_list_view_container)
    LoadMoreListViewContainer loadMoreListViewContainer;
    @Bind(R.id.ptr_frame)
    PtrFrameLayout ptrFrame;
    @Bind(R.id.tv_empty)
    TextView tvEmpty;
    @Bind(R.id.ll_empty)
    LinearLayout llEmpty;
    @Bind(R.id.tv_subject_type)
    TextView tvSubjectType;
    @Bind(R.id.tv_difficulty)
    TextView tvDifficulty;
    @Bind(R.id.tv_unsel)
    TextView tvUnsel;
    @Bind(R.id.ll_screen)
    LinearLayout llScreen;


    JobSelAdapter adapter;
    List<JobSelInfo> list;


    private String type = "";
    private String id = "";
    private int page = 1;
    private String types = "";//题目类型
    private String star = "";//难度星级
    private String already = "";//未出过

    PopupWindow popupWindow1;
    PopupWindow popupWindow2;
    PopupWindow popupWindow3;
    List<ChooseTypesInfo> typesList;
    List<ChooseTypesInfo> starList;
    List<ChooseTypesInfo> alreadyList;


    JobSelInfo item;//临时点击的item

    @Override
    public int getLayoutId() {
        return R.layout.job_sel_aty;
    }

    @Override
    public void initData() {
        if (getIntent().getExtras() != null) {
            type = getIntent().getStringExtra("type");
            id = getIntent().getStringExtra("id");
        }
        typesList = new ArrayList<>();
        starList = new ArrayList<>();
        alreadyList = new ArrayList<>();
        PtrInitHelper.initPtr(this, ptrFrame);
        ptrFrame.setPtrHandler(this);
        loadMoreListViewContainer.useDefaultFooter();
        loadMoreListViewContainer.setAutoLoadMore(true);
        loadMoreListViewContainer.loadMoreFinish(false, true);
        loadMoreListViewContainer.setLoadMoreHandler(this);
        listview.setFooterDividersEnabled(false);
        listview.setEmptyView(llEmpty);
        tvEmpty.setText("暂无改类型题目");

        list = new ArrayList<>();
        adapter = new JobSelAdapter(this, list, R.layout.job_sel_item);
        listview.setAdapter(adapter);

        fbtnPreview.setText("已选" + UserManger.subject_id.size() + "道题，点击预览");
    }

    @Override
    public void requestData() {
        showLoadingContent();
        if (type.equals("3")){//搜索
            new Teacher().fuzzy_search(id,page+"",this,2);
            llScreen.setVisibility(View.GONE);
        }else{
            new Teacher().chooseQuestion(type, id, page + "",UserManger.getId(),types,star,already, this, 0);
            llScreen.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void intoActivity(Object object, int what) {
        if (what == 0) {
            item = (JobSelInfo) object;
            fbtnPreview.setText("已选" + UserManger.subject_id.size() + "道题，点击预览");
        }
        super.intoActivity(object, what);
    }

    @OnClick({R.id.iv_back, R.id.fbtn_preview,R.id.ll_but,R.id.ll_difficulty,R.id.ll_unsel})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.fbtn_preview:
                if (Toolkit.listIsEmpty(UserManger.subject_id)) {
                    showToast("请选择题目");
                    return;
                }
                startActivity(PreviewAty.class, null);
                break;
            case R.id.ll_but:
                showLoadingDialog("");
                new Teacher().choose_types(this,4);
                break;
            case R.id.ll_difficulty://难度
                showLoadingDialog("");
                new Teacher().choose_stars(this,5);
                break;
            case R.id.ll_unsel://未选过
                showLoadingDialog("");
                new Teacher().choose_already(this,6);
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what) {
            case 0:
                list.clear();
                list.addAll(AppJsonUtil.getArrayList(result, JobSelInfo.class));
                adapter.notifyDataSetChanged();
                for (int i = 0; i < list.size(); i++) {
                    if (UserManger.subject_id.contains(list.get(i).getId())) {
                        list.get(i).setIs_selector(true);
                    }
                }
                ptrFrame.refreshComplete();
                loadMoreListViewContainer.loadMoreFinish(false, true);
                break;
            case 1:
                if (Toolkit.listIsEmpty(AppJsonUtil.getArrayList(result, JobSelInfo.class))) {
                    loadMoreListViewContainer.loadMoreFinish(false, false);
                } else {
                    page++;
                    list.addAll(AppJsonUtil.getArrayList(result, JobSelInfo.class));
                    for (int i = 0; i < list.size(); i++) {
                        if (UserManger.subject_id.contains(list.get(i).getId())) {
                            list.get(i).setIs_selector(true);
                        }
                    }
                    adapter.notifyDataSetChanged();
                    loadMoreListViewContainer.loadMoreFinish(false, true);
                }
                break;
            case 2:
                list.clear();
                list.addAll(AppJsonUtil.getArrayList(result, JobSelInfo.class));
                adapter.notifyDataSetChanged();
                for (int i = 0; i < list.size(); i++) {
                    if (UserManger.subject_id.contains(list.get(i).getId())) {
                        list.get(i).setIs_selector(true);
                    }
                }
                ptrFrame.refreshComplete();
                loadMoreListViewContainer.loadMoreFinish(false, true);
                break;
            case 3:
                if (Toolkit.listIsEmpty(AppJsonUtil.getArrayList(result, JobSelInfo.class))) {
                    loadMoreListViewContainer.loadMoreFinish(false, false);
                } else {
                    page++;
                    list.addAll(AppJsonUtil.getArrayList(result, JobSelInfo.class));
                    for (int i = 0; i < list.size(); i++) {
                        if (UserManger.subject_id.contains(list.get(i).getId())) {
                            list.get(i).setIs_selector(true);
                        }
                    }
                    adapter.notifyDataSetChanged();
                    loadMoreListViewContainer.loadMoreFinish(false, true);
                }
                break;
            case 4:
                typesList.clear();
                typesList.addAll(AppJsonUtil.getArrayList(result,ChooseTypesInfo.class));
                screen1();
                break;
            case 5:
                starList.clear();
                starList.addAll(AppJsonUtil.getArrayList(result,ChooseTypesInfo.class));
                screen2();
                break;
            case 6:
                alreadyList.clear();
                alreadyList.addAll(AppJsonUtil.getArrayList(result,ChooseTypesInfo.class));
                screen3();
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(ptrFrame, listview, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        page = 1;
        if (type.equals("3")){
            new Teacher().fuzzy_search(id,page+"",this,2);
        }else{
            new Teacher().chooseQuestion(type, id, page + "",UserManger.getId(),types,star,already, this, 0);
        }

    }

    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
        if (type.equals("3")){
            new Teacher().fuzzy_search(id,(page+1)+"",this,3);
        }else {
            new Teacher().chooseQuestion(type, id, (page + 1) + "",UserManger.getId(),types,star,already, this, 1);
        }
    }


    private void screen1() {
        if (popupWindow1 == null) {
            popupWindow1 = new PopupWindow(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(this).inflate(R.layout.window_screen_rule_layout, null);
            popupWindow1.setContentView(view);
            View tvBack =  view.findViewById(R.id.tv_back);
            tvBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow1.dismiss();
                }
            });
            ListView mlistView = (ListView) view.findViewById(R.id.listview);
            final List<String> list = new ArrayList<>();
            for (int i = 0;i<typesList.size();i++){
                list.add(typesList.get(i).getTitle());
            }
            final ScreenRuleAdapter adapter = new ScreenRuleAdapter(this, list, R.layout.textview_layout);
            mlistView.setAdapter(adapter);
            popupWindow1.setBackgroundDrawable(new ColorDrawable());
            popupWindow1.setOutsideTouchable(true);

            mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    adapter.setChooseIndex(i);
                    adapter.notifyDataSetChanged();
                    page = 0;
                    tvSubjectType.setText(typesList.get(i).getTitle());
                    types = typesList.get(i).getType();
                    showLoadingDialog("");
                    new Teacher().chooseQuestion(type, id, page + "",UserManger.getId(),types,star,already, JobSelAty.this, 0);
                    popupWindow1.dismiss();
                }
            });

            popupWindow1.showAsDropDown(llBut);
        } else {
            popupWindow1.showAsDropDown(llBut);

        }

    }

    private void screen2() {
        if (popupWindow2 == null) {
            popupWindow2 = new PopupWindow(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(this).inflate(R.layout.window_screen_rule_layout, null);
            popupWindow2.setContentView(view);
            View tvBack =  view.findViewById(R.id.tv_back);
            tvBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow2.dismiss();
                }
            });
            ListView mlistView = (ListView) view.findViewById(R.id.listview);
            final List<String> list = new ArrayList<>();
            for (int i = 0;i<starList.size();i++){
                list.add(starList.get(i).getTitle());
            }
            final ScreenRuleAdapter adapter = new ScreenRuleAdapter(this, list, R.layout.textview_layout);
            mlistView.setAdapter(adapter);
            popupWindow2.setBackgroundDrawable(new ColorDrawable());
            popupWindow2.setOutsideTouchable(true);

            mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    adapter.setChooseIndex(i);
                    adapter.notifyDataSetChanged();
                    page = 0;
                    tvDifficulty.setText(starList.get(i).getTitle());
                    star = starList.get(i).getType();
                    showLoadingDialog("");
                    new Teacher().chooseQuestion(type, id, page + "",UserManger.getId(),types,star,already, JobSelAty.this, 0);
                    popupWindow2.dismiss();
                }
            });

            popupWindow2.showAsDropDown(llDifficulty);
        } else {
            popupWindow2.showAsDropDown(llDifficulty);

        }

    }

    private void screen3() {
        if (popupWindow3 == null) {
            popupWindow3 = new PopupWindow(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(this).inflate(R.layout.window_screen_rule_layout, null);
            popupWindow3.setContentView(view);
            View tvBack =  view.findViewById(R.id.tv_back);
            tvBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow3.dismiss();
                }
            });
            ListView mlistView = (ListView) view.findViewById(R.id.listview);
            final List<String> list = new ArrayList<>();
            for (int i = 0;i<alreadyList.size();i++){
                list.add(alreadyList.get(i).getTitle());
            }
            final ScreenRuleAdapter adapter = new ScreenRuleAdapter(this, list, R.layout.textview_layout);
            mlistView.setAdapter(adapter);
            popupWindow3.setBackgroundDrawable(new ColorDrawable());
            popupWindow3.setOutsideTouchable(true);

            mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    adapter.setChooseIndex(i);
                    adapter.notifyDataSetChanged();
                    page = 0;
                    tvUnsel.setText(alreadyList.get(i).getTitle());
                    already = alreadyList.get(i).getType();
                    showLoadingDialog("");
                    new Teacher().chooseQuestion(type, id, page + "",UserManger.getId(),types,star,already, JobSelAty.this, 0);
                    popupWindow3.dismiss();
                }
            });

            popupWindow3.showAsDropDown(llUnsel);
        } else {
            popupWindow3.showAsDropDown(llUnsel);

        }

    }

}
